package main

import (
	"log"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

func Logger() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		t := time.Now()
		log.Println(t)
		ctx.Set("user", "leorunloop")
		ctx.Next()
		latency := time.Since(t)
		log.Println(latency)
		status := ctx.Writer.Status()
		log.Println(status)
	}
}

func main() {
	router := gin.New()
	router.Use(Logger())
	router.GET("/custom-middleware", func(ctx *gin.Context) {
		message := ctx.MustGet("user")
		ctx.JSON(http.StatusOK, gin.H{
			"message": message,
		})
	})
	router.Run()
}
